package com.sherlocky.util;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import com.sherlocky.resource.common.ResourceContants;

public class FileUtils {
    /**
     * The number of bytes in a kilobyte.
     */
    public static final long ONE_KB = 1024;

    /**
     * The number of bytes in a megabyte.
     */
    public static final long ONE_MB = ONE_KB * ONE_KB;

    /**
     * The number of bytes in a gigabyte.
     */
    public static final long ONE_GB = ONE_KB * ONE_MB;
    
    //-----------------------------------------------------------------------
    /**
     * Returns a human-readable version of the file size, where the input
     * represents a specific number of bytes.
     *
     * @param size  the number of bytes
     * @return a human-readable display value (includes units)
     */
    public static String byteCountToDisplaySize(long size) {
        String displaySize;

        if (size / ONE_GB > 0) {
            displaySize = String.valueOf(size / ONE_GB) + " GB";
        } else if (size / ONE_MB > 0) {
            displaySize = String.valueOf(size / ONE_MB) + " MB";
        } else if (size / ONE_KB > 0) {
            displaySize = String.valueOf(size / ONE_KB) + " KB";
        } else {
            displaySize = String.valueOf(size) + " B";
        }
        return displaySize;
		/*
		DecimalFormat df = new DecimalFormat("###.0");
		//# 代表一个数字,不包括0; 0 代表一个数字
		float f;
		if(this.fileSizeBytes < 1000) {
			return this.fileSizeBytes + " B";
		} else if(this.fileSizeBytes < 1000*1000) {
			f = (float)((float)this.fileSizeBytes/(float)1000);
			return (df.format(new Float(f).doubleValue()) + " KB");
		} else if(this.fileSizeBytes < 1000*1000*1000) {
			f = (float)((float)this.fileSizeBytes/(float)(1000*1000));
			return (df.format(new Float(f).doubleValue()) + " MB");
		} else {
			f = (float)((float)this.fileSizeBytes/(float)(1000*1000*1000));
			return (df.format(new Float(f).doubleValue()) + " GB");
		}
		*/        
    }
    
	public static String getFileType(String suffix) {
		return getFileTypeEnum(suffix).toString();
	}	
	
	public static ResourceContants.FileType getFileTypeEnum(String suffix) {
		if (ArrayUtils.contains(ResourceContants.IMAGE_TYPES, suffix)) {//图片类型
			return ResourceContants.FileType.IMAGE;
		} else if(ArrayUtils.contains(ResourceContants.OFFICE_TYPES, suffix)) {//文档类型
			return ResourceContants.FileType.OFFICE;
		} else if(ArrayUtils.contains(ResourceContants.VIDEO_TYPES, suffix)) {//视频类型
			return ResourceContants.FileType.VIDEO;
		} else if(ArrayUtils.contains(ResourceContants.AUDIO_TYPES, suffix)) {//视频类型
			return ResourceContants.FileType.AUDIO;
		}
		return ResourceContants.FileType.UNKNOWN;
	}
	
	/**
	 * 根据后缀判定是否是图片
	 * @param suffix
	 * @return
	 */
	public static boolean isImage(String suffix) {
		if (StringUtils.isBlank(suffix)) {
			return false;
		}
		return ArrayUtils.contains(ResourceContants.IMAGE_TYPES, suffix.toLowerCase());
	}
	
	/**
	 * 判断是否为视频
	 * @param suffix
	 * @return
	 */
	public static boolean isVideo(String suffix) {
		if (StringUtils.isBlank(suffix)) {
			return false;
		}
		return ArrayUtils.contains(ResourceContants.VIDEO_TYPES, suffix.toLowerCase());
	}
	
	/**
	 * 判断是否为音频
	 * @param suffix
	 * @return
	 */
	public static boolean isAudio(String suffix) {
		if (StringUtils.isBlank(suffix)) {
			return false;
		}
		return ArrayUtils.contains(ResourceContants.AUDIO_TYPES, suffix.toLowerCase());
	}
	
	/**
	 * 判断是否为office文件 用于在线观看
	 * @param suffix
	 * @return
	 */
	public static boolean isOffice(String suffix){
		if (StringUtils.isBlank(suffix)) {
			return false;
		}
		return ArrayUtils.contains(ResourceContants.OFFICE_TYPES, suffix.toLowerCase());
	}

	public static String getSuffix(String fileName) {
		return StringUtils.substringAfterLast(fileName, ".");
	}
}
